package com.eightywork.life.activity;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.TelephoneListAdapter;
import com.eightywork.life.model.Telephone;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.DoService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

public class TelephoneActivity extends BaseActivity implements OnClickListener {
	private ImageButton rightButton,leftButton;
	private ListView listView;
	private TelephoneListAdapter adapter;
	private List<Telephone> list = new ArrayList<Telephone>();
	private TextView headerTitle;
	private AsyncTask<?, ?, ?> task;
	private int page = 1;
	private boolean isLoading;
	private ViewGroup footView;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_telephone);
		initView();
		initData();
	}

	@Override
	public void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}


	private void initView() {
		initHeader("便民电话");
		headerTitle = (TextView) findViewById(R.id.header_title);
		rightButton = (ImageButton) findViewById(R.id.button_right);
		leftButton = (ImageButton) findViewById(R.id.button_left);
		listView = (ListView) findViewById(R.id.listView1);
		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);
		headerTitle.setOnClickListener(this);
		footView = (ViewGroup) getLayoutInflater().inflate(
				R.layout.item_footer, null);
		listView.addFooterView(footView);
	}

	private void initData() {
		adapter = new TelephoneListAdapter(context, list);
		listView.setAdapter(adapter);
		listView.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					if (view.getLastVisiblePosition() == (view.getCount() - 1)
							&& !isLoading) {
						getList();
					}
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

			}
		});
		getList();
	}

	public void getList() {
			task = new ListTask().execute();
	}

	class ListTask extends AsyncTask<Void, Void, String> {
		private List<Telephone> aList;

		@Override
		protected void onPreExecute() {
			isLoading = true;
			footView.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			try {
				if(CacheService.isLogined()) {
				    aList = SpaceService.teleList(CacheService.getSpace().getXiaoquid(), page+"", "");
				} else {
					aList = SpaceService.teleList( getIntent().getStringExtra("xiaoquid"),page+"", "");
				}
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result == null) {
				if (aList != null) {
					list.addAll(aList);
				}
				adapter.notifyDataSetChanged();
			} else {
				showMessage(result);
			}
			footView.setVisibility(View.GONE);
			isLoading = false;
			page++;
			super.onPostExecute(result);
		}
	}


	@Override
	public void onClick(View v) {
		if(leftButton == v){
			finish();
		}
	}
}
